507c6b
@@ -233,18 +233,30 @@
private void registerAllFunctionsOnce() throws HiveException {
 
 
   public void reloadFunctions() throws HiveException {
+    HashSet<String> registryFunctions = new HashSet<String>(
+        FunctionRegistry.getFunctionNames(".+\\..+"));
     for (Function function : getAllFunctions()) {
       String functionName = function.getFunctionName();
       try {
         LOG.info("Registering function " + functionName + " " + function.getClassName());
-        FunctionRegistry.registerPermanentFunction(FunctionUtils.qualifyFunctionName(
-                    functionName, function.getDbName()), function.getClassName(), false,
+        String qualFunc = FunctionUtils.qualifyFunctionName(functionName, function.getDbName());
+        FunctionRegistry.registerPermanentFunction(qualFunc, function.getClassName(), false,
                     FunctionTask.toFunctionResource(function.getResourceUris()));
+        registryFunctions.remove(qualFunc);
       } catch (Exception e) {
         LOG.warn("Failed to register persistent function " +
                 functionName + ":" + function.getClassName() + ". Ignore and continue.");
       }
     }
+    // unregister functions from local system registry that are not in getAllFunctions()
+    for (String functionName : registryFunctions) {
+      try {
+        FunctionRegistry.unregisterPermanentFunction(functionName);
+      } catch (Exception e) {
+        LOG.warn("Failed to unregister persistent function " +
+            functionName + "on reload. Ignore and continue.");
+      }
+    }
   }
 
   public static Hive get(Configuration c, Class<?> clazz) throws HiveException {
